package com.warmheart.core.util;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.warmheart.core.ex.ServerException;

/**
 * 
 * @ClassName: JsonResponseUtil
 * @Description: JSON字符串响应工具类
 * @author ZhangJianyang
 * @date 2024年3月7日 下午2:22:54
 *
 */
public class JsonResponseUtil {

    public static void writerJsonFromObj(HttpServletResponse response, Object object) {
        try {
            writerJsonStr(response, JsonUtil.getJsonStr(object));
        } catch (Exception e) {
            if (e instanceof ServerException) {
                // 服务异常直接抛出
                throw (ServerException) e;
            } else {
                // 其余异常均整理为服务异常抛出
                throw new ServerException("JSON转换异常", e);
            }
        }
    }

    /**
     * JSON返回数据
     * 
     * @param response
     * @param msg
     * @param state
     */
    public static void writerJson(HttpServletResponse response, String msg, boolean state) {
        try {
            writerJson(response, msg, state, null);
        } catch (Exception e) {
            if (e instanceof ServerException) {
                // 服务异常直接抛出
                throw (ServerException) e;
            } else {
                // 其余异常均整理为服务异常抛出
                throw new ServerException("JSON转换异常", e);
            }
        }
    }

    /**
     * JSON返回数据
     * 
     * @param response
     * @param msg
     * @param state
     * @param object
     */
    public static void writerJson(HttpServletResponse response, String msg, boolean state, Object object) {
        try {
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("state", state);
            if (!StringUtils.isBlank(msg)) {
                result.put("msg", msg);
            }
            if (object != null) {
                result.put("data", object);
            }
            writerJsonStr(response, JsonUtil.getJsonStr(result));
        } catch (Exception e) {
            if (e instanceof ServerException) {
                // 服务异常直接抛出
                throw (ServerException) e;
            } else {
                // 其余异常均整理为服务异常抛出
                throw new ServerException("JSON转换异常", e);
            }
        }
    }

    /**
     *
     * @param response
     * @param msg
     * @param code
     * @param state
     * @param object
     */
    public static void writerJson(HttpServletResponse response, String msg, Integer code, boolean state, Object object) {
        try {
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("state", state);
            if (!StringUtils.isBlank(msg)) {
                result.put("msg", msg);
            }
            if (object != null) {
                result.put("data", object);
            }
            if (null != code) {
                result.put("code", code);
            }
            writerJsonStr(response, JsonUtil.getJsonStr(result));
        } catch (Exception e) {
            if (e instanceof ServerException) {
                // 服务异常直接抛出
                throw (ServerException) e;
            } else {
                // 其余异常均整理为服务异常抛出
                throw new ServerException("JSON转换异常", e);
            }
        }
    }

    public static void writerJsonStr(HttpServletResponse response, String jsonStr) {
        PrintWriter out = null;
        try {
            // 清空响应缓存
            response.reset();
            // 设置状态码为200，标识响应成功
            response.setStatus(200);
            // 设置响应内容类型
            response.setContentType("application/json;charset=utf-8");
            // 设置页面不缓存
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            // 设置内容编码格式
            response.setCharacterEncoding("UTF-8");
            // 获取字符输出流
            out = response.getWriter();
            // 写出字符串数据
            out.print(jsonStr);
            // 缓存全部写出
            out.flush();
        } catch (Exception e) {
            if (e instanceof ServerException) {
                // 服务异常直接抛出
                throw (ServerException) e;
            } else {
                // 其余异常均整理为服务异常抛出
                throw new ServerException("JSON转换异常", e);
            }
        } finally {
            // 关闭字符输出流
            if (out != null) {
                out.close();
            }
        }
    }
}